var TuyenSinh = function () {
	var HanderInputDiem = function(){
		jQuery("input[name='txtHK1_11_Mon1']").inputmask("9{2}.9{2}");
		jQuery("input[name='txtHK1_11_Mon2']").inputmask("9{2}.9{2}");
		jQuery("input[name='txtHK1_11_Mon3']").inputmask("9{2}.9{2}");
		// Học kỳ II 11
		jQuery("input[name='txtHK2_11_Mon1']").inputmask("9{2}.9{2}");
		jQuery("input[name='txtHK2_11_Mon2']").inputmask("9{2}.9{2}");
		jQuery("input[name='txtHK2_11_Mon3']").inputmask("9{2}.9{2}");
		//Học kỳ I 12
		jQuery("input[name='txtHK1_12_Mon1']").inputmask("9{2}.9{2}");
		jQuery("input[name='txtHK1_12_Mon2']").inputmask("9{2}.9{2}");
		jQuery("input[name='txtHK1_12_Mon3']").inputmask("9{2}.9{2}");
		// Điểm chuẩn
		jQuery("input[name='txtDiemXetTuyen']").inputmask("9{2}.9{2}");
	}
	var Select2Dropdown = function(){
		jQuery("select[name='noisinh']").select2();
		jQuery("select[name='doituong']").select2();
		jQuery("select[name='khuvuc']").select2();
		jQuery("select[name='tinhthanh']").select2();
		jQuery("select[name='quanhuyen']").select2();
		jQuery("select[name='thpt']").select2();
		jQuery("select[name='nganhnghe']").select2();
		jQuery("select[name='KhoiXet']").select2();
	}
	var HanderDateBootstrap = function(){
		jQuery("input[name='txtNgaySinh']").datepicker({format:'dd-mm-yyyy'});
		jQuery("input[name='txtNgayNhanHoSo']").datepicker({format:'dd-mm-yyyy'});
	}
	// Hander ajax 
	var HanderLoadQuanHuyenByidTinh = function(idTinh){	
		var _token = jQuery("input[name='_token']").val();
		jQuery.ajax({
			"url":baseURL+"/ajax/load-quan-huyen",
			"type":"POST",
			"data":{"_token":_token,"idTinh":idTinh},
			"async":true,
			"success":function(data){
				jQuery("#quanhuyen").html(data);
			}
		});
	}
	var HanderLoadTinhThanhByidTinh = function(idTinh){	
		var _token = jQuery("input[name='_token']").val();
		jQuery.ajax({
			"url":baseURL+"/ajax/load-quan-huyen",
			"type":"POST",
			"data":{"_token":_token,"idTinh":idTinh},
			"async":true,
			"success":function(data){
				jQuery("#quanhuyen").html(data);
			}
		});
	}
	var HanderLoadTruongTHPTByidTinh = function(idTinh){	
		var _token = jQuery("input[name='_token']").val();
		jQuery.ajax({
			"url":baseURL+"/ajax/load-truong-trung-hoc-pho-thong",
			"type":"POST",
			"data":{"_token":_token,"idTinh":idTinh},
			"async":true,
			"success":function(data){
				jQuery("#thpt").html(data);
			}
		});
	}
	var HanderLoadNganhNgheByidNganh = function(idNganh){
		var _token = jQuery("input[name='_token']").val();
		var _html = "";
		var DotXet = jQuery("select[name='DotXet']").val();
		jQuery.ajax({
			"url":baseURL+"/ajax/load-nganh-nghe",
			"type":"POST",
			"data":{"_token":_token,"idNganh":idNganh},
			"async":true,
			"success":function(data){
				var getData = jQuery.parseJSON(data);
				jQuery("input[name='txtTenNganh']").val(getData.Name);
				// Xử lý khối xét
				var khoixet_new = getData.KhoiXetTuyen.split(',');
				// Duyệt mảng
				_html += "<option value=''>Khối xét</option>";
				jQuery.each(khoixet_new, function(key, value) {
					_html += "<option value='"+value+"'>"+value+"</option>";
				});
				jQuery("select[name='KhoiXet']").html(_html);
				// End xử lý khối xét
				switch (parseInt(DotXet)){
					case 2:
						jQuery("input[name='txtDiemXetTuyen']").val(getData.Dot2);
						jQuery("input[name='diemchuan']").val(getData.Dot2);
						break;
					case 3:
						jQuery("input[name='txtDiemXetTuyen']").val(getData.Dot3);
						jQuery("input[name='diemchuan']").val(getData.Dot3);
						break;
					case 4:
						jQuery("input[name='txtDiemXetTuyen']").val(getData.Dot4);
						jQuery("input[name='diemchuan']").val(getData.Dot4);
						break;
					case 5:
						jQuery("input[name='txtDiemXetTuyen']").val(getData.Dot5);
						jQuery("input[name='diemchuan']").val(getData.Dot5);
						break;
					default:
						jQuery("input[name='txtDiemXetTuyen']").val(getData.Dot1);
						jQuery("input[name='diemchuan']").val(getData.Dot1);
				}
			}
		});
	}
	var HanderLoadNameKhoiXet = function(KhoiXet){
		var _token = jQuery("input[name='_token']").val();
		jQuery.ajax({
			"url":baseURL+"/ajax/load-name-khoi-xet",
			"type":"POST",
			"data":{"_token":_token,"KhoiXet":KhoiXet},
			"async":true,
			"success":function(data){
				var getData = jQuery.parseJSON(data);
				jQuery("#Name_Mon1").html(getData.Mon1);
				jQuery("#Name_Mon2").html(getData.Mon2);
				jQuery("#Name_Mon3").html(getData.Mon3);
			}
		});
	}
	// End Hander ajax
	// Hander dữ liệu
	var HanderTinhThanhSelected = function(idTinh){
		if(idTinh == ''){
			document.getElementById("quanhuyen").disabled = true;
			document.getElementById("thpt").disabled = true;
		}else{
			document.getElementById("quanhuyen").disabled = false;
			document.getElementById("thpt").disabled = false;
			// ajax
			HanderLoadQuanHuyenByidTinh(idTinh);
			HanderLoadTruongTHPTByidTinh(idTinh);
		}
	}

	var HanderNganhNgheSelected = function(idNganh){
		if(idNganh == ''){
			document.getElementById("KhoiXet").disabled = true;
			document.getElementById("NamTN").disabled = true;
			jQuery("input[name='txtTenNganh']").attr('readonly', true);
			// Điểm xét
			jQuery("input[name='txtDiemXetTuyen']").attr('readonly', true);
			jQuery("input[name='txtKetQua']").attr('readonly', true);
		}else{
			document.getElementById("KhoiXet").disabled = false;
			document.getElementById("NamTN").disabled = false;
			jQuery("input[name='txtTenNganh']").attr('readonly', false);
			// Điểm xét
			jQuery("input[name='txtDiemXetTuyen']").attr('readonly', false);
			jQuery("input[name='txtKetQua']").attr('readonly', false);
			// Phần nganh
			HanderLoadNganhNgheByidNganh(idNganh);
		}
	}
	var HanDerKhoiXetSelected = function(KhoiXet){
		if(KhoiXet == ''){
			// Học kỳ I 11
			jQuery("input[name='txtHK1_11_Mon1']").attr('readonly', true);
			jQuery("input[name='txtHK1_11_Mon2']").attr('readonly', true);
			jQuery("input[name='txtHK1_11_Mon3']").attr('readonly', true);
			// Học kỳ II 11
			jQuery("input[name='txtHK2_11_Mon1']").attr('readonly', true);
			jQuery("input[name='txtHK2_11_Mon2']").attr('readonly', true);
			jQuery("input[name='txtHK2_11_Mon3']").attr('readonly', true);
			// Học kỳ I 12
			jQuery("input[name='txtHK1_12_Mon1']").attr('readonly', true);
			jQuery("input[name='txtHK1_12_Mon2']").attr('readonly', true);
			jQuery("input[name='txtHK1_12_Mon3']").attr('readonly', true);
		}else{
			// Học kỳ I 11
			jQuery("input[name='txtHK1_11_Mon1']").attr('readonly', false);
			jQuery("input[name='txtHK1_11_Mon2']").attr('readonly', false);
			jQuery("input[name='txtHK1_11_Mon3']").attr('readonly', false);
			// Học kỳ II 11
			jQuery("input[name='txtHK2_11_Mon1']").attr('readonly', false);
			jQuery("input[name='txtHK2_11_Mon2']").attr('readonly', false);
			jQuery("input[name='txtHK2_11_Mon3']").attr('readonly', false);
			// Học kỳ I 12
			jQuery("input[name='txtHK1_12_Mon1']").attr('readonly', false);
			jQuery("input[name='txtHK1_12_Mon2']").attr('readonly', false);
			jQuery("input[name='txtHK1_12_Mon3']").attr('readonly', false);
			// Ajax khối xét
			HanderLoadNameKhoiXet(KhoiXet);
		}
	}
	// Xử lý dữ liệu
	var TinhThanhSelected = function(){
		var idTinh = jQuery("select[name='tinhthanh']").val();
		// Chưa change
		HanderTinhThanhSelected(idTinh);
		// Change select
		jQuery("select[name='tinhthanh']").on("change",function(){
			var idTinh = jQuery(this).val();
			HanderTinhThanhSelected(idTinh);
			console.log(jQuery("input[name='txtNgaySinh']"));
		});
	}
	var NganhNgheSelected = function(){
		var idNganh = jQuery("select[name='nganhnghe']").val();
		// Chưa change
		HanderNganhNgheSelected(idNganh);
		// Change select
		jQuery("select[name='nganhnghe']").on("change",function(){
			var idNganh = jQuery(this).val();
			HanderNganhNgheSelected(idNganh);
		});
	}
	var KhoiXetSelected = function(KhoiXet){
		var KhoiXet = jQuery("select[name='KhoiXet']").val();
		// Chưa change
		HanDerKhoiXetSelected(KhoiXet);
		// Change select
		jQuery("select[name='KhoiXet']").on("change",function(){
			var KhoiXet = jQuery(this).val();
			HanDerKhoiXetSelected(KhoiXet);
		});
	}
	// End Xử Lý dữ liệu
	return{
		init: function(){
			HanderInputDiem();
			Select2Dropdown();
			HanderDateBootstrap();
			TinhThanhSelected();
			NganhNgheSelected();
			KhoiXetSelected();
		},
	}
}();